package com.zcs.app.base.asm

import android.app.Activity
import java.util.*

object ActivityStackManger {
    private val mActivityStack by lazy {
        Stack<Activity?>()
    }

    fun getStack(): Stack<Activity?> {
        return mActivityStack
    }

    /**
     * 入栈
     */
    fun addActivity(activity: Activity?) {
        mActivityStack.push(activity)
    }

    /**
     * 出栈
     */
    fun removeActivity(activity: Activity?) {
        mActivityStack.remove(activity)
    }

    /**
     * 获取当前的Activity(最后一个入栈的Activity)
     */
    fun getCurrentActivity(): Activity? {
        return if (mActivityStack.size == 0) null else mActivityStack.lastElement()
    }

    /**
     * 结束当前Activity（最后一个入栈的Activity）
     */
    fun finishCurrentActivity() {
        val activity = mActivityStack.lastElement()
        activity?.finish()
    }

    /**
     * 结束所有Activity
     */
    fun finishAllActivity() {
        var activity: Activity?
        while (!mActivityStack.empty()) {
            activity = mActivityStack.pop()
            activity?.finish()
        }
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(activity: Activity?): Boolean {
        if (null != activity) {
            mActivityStack.remove(activity)
            if (!activity.isFinishing) activity.finish()
            return true
        }
        return false
    }

    /**
     * 结束指定的Activity
     */
    fun finishActivity(actCls: Class<out Activity?>): Boolean {
        val activity = findActivityByClass(actCls)
        return finishActivity(activity)
    }

    /**
     * 根据类名查找Activity
     */
    fun findActivityByClass(actCls: Class<out Activity?>): Activity? {
        var tempActivity: Activity? = null
        val itr: Iterator<Activity?> = mActivityStack.iterator()
        while (itr.hasNext()) {
            tempActivity = itr.next()
            if (null != tempActivity && tempActivity.javaClass.name == actCls.name) {
                break
            }
            tempActivity = null
        }
        return tempActivity
    }

    /**
     * finish指定的Activity之上的所有Activity
     *
     * @param actCls        目标Activity
     * @param isIncludeSelf 是否关闭当前位于栈顶的Activity
     * @return
     */
    fun finishToActivity(actCls: Class<out Activity?>, isIncludeSelf: Boolean): Boolean {
        val buf: MutableList<Activity?> = ArrayList()
        val size = mActivityStack.size
        var activity: Activity?
        for (i in size - 1 downTo 0) {
            activity = mActivityStack[i]
            if (activity?.javaClass?.isAssignableFrom(actCls) == true) {
                for (a in buf) {
                    a?.finish()
                }
                return true
            } else if (i == size - 1 && isIncludeSelf) {
                buf.add(activity)
            } else if (i != size - 1) {
                buf.add(activity)
            }
        }
        return false
    }

    /**
     * 指定的activity实例是否存活
     *
     * @param activity
     * @return
     */
    fun activityInstanceIsLive(activity: Activity?): Boolean {
        return if (mActivityStack.isEmpty()) {
            false
        } else mActivityStack.contains(activity)
    }

    /**
     * 指定的activity实例是否存活
     *
     * @param actCls
     * @return
     */
    fun activityInstanceIsLive(actCls: Class<out Activity?>): Boolean {
        return if (mActivityStack.isEmpty()) {
            false
        } else mActivityStack.contains(findActivityByClass(actCls))
    }

    /**
     * 退出应用程序
     */
    fun exitApp() {
        finishAllActivity()
//        android.os.Process.killProcess(android.os.Process.myPid());
//        System.gc();
    }

}
